package blog

import (
	"GinBlog/global"
	"GinBlog/models"
	"GinBlog/models/request"
	"GinBlog/models/response"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

type CommentApi struct{}

// Create
// @Tags      评论
// @Summary   创建评论
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.CreateCommentRequest true "评论信息"
// @Success   200   {object}  response.Response{data=object} "添加成功"
// @Router    /blog/comment/ [post]
func (u *CommentApi) Create(c *gin.Context) {
	var req request.CreateCommentRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}

	v, exits := c.Get("UserId")
	if exits == false {
		global.Logger.Info("用户不存在或未登录")
		response.FailWithMessage("用户不存在或未登录", c)
	}

	userid := v.(uint)
	comment := &models.Comment{
		Content: req.Content,
		UserID:  &userid,
		PostID:  &req.PostId,
	}
	commentService.Create(comment)
	response.OkWithDetailed(comment, "添加成功", c)
}

// Update
// @Tags      评论
// @Summary   更新评论
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.UpdateCommentRequest true "评论信息"
// @Success   200   {object}  response.Response{msg=string} "更新成功"
// @Router    /blog/comment/ [put]
func (u *CommentApi) Update(c *gin.Context) {
	var req request.UpdateCommentRequest
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
	}

	v, exits := c.Get("UserId")
	if exits == false {
		global.Logger.Info("用户不存在或未登录")
		response.FailWithMessage("用户不存在或未登录", c)
	}
	userid := v.(uint)

	comment := &models.Comment{
		Content: req.Content,
		Model: gorm.Model{
			ID: req.Id,
		},
	}
	err = commentService.Update(comment, userid)
	if err != nil {
		global.Logger.Error("评论更新失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	}
	response.OkWithMessage("更新成功", c)
}

// Delete
// @Tags      评论
// @Summary   删除评论
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     commentId  path      uint  true  "评论ID"
// @Success   200   {object}  response.Response{msg=string} "删除成功"
// @Router    /blog/comment/{commentId} [delete]
func (u *CommentApi) Delete(c *gin.Context) {
	commentIdStr := c.Param("commentId") // 获取路径参数
	// 转换为uint类型
	commentId, err := strconv.ParseUint(commentIdStr, 10, 32)
	if err != nil {
		response.FailWithMessage("参数格式错误", c)
		return
	}

	v, exits := c.Get("UserId")
	if exits == false {
		global.Logger.Info("用户不存在或未登录")
		response.FailWithMessage("用户不存在或未登录", c)
	}
	userid := v.(uint)

	err = commentService.Delete(uint(commentId), userid)
	if err != nil {
		global.Logger.Error("评论删除失败", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	}
	response.OkWithMessage("删除成功", c)
}

// List
// @Tags      评论
// @Summary   获取评论列表
// @Security  ApiKeyAuth
// @accept    application/json
// @Produce   application/json
// @Param     data  body      request.ListCommentRequest true "分页参数"
// @Success   200   {object}  response.Response{data=response.PageResult,msg=string} "查询成功"
// @Router    /blog/comment/list [post]
func (u *CommentApi) List(c *gin.Context) {
	var pageInfo request.ListCommentRequest
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := commentService.List(pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "查询成功", c)
}
